package atom.ui 
{
	import atom.ui.Component;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import com.greensock.easing.Bounce;
	import com.greensock.TweenLite;
	
	/**
	 * Implements generic component container logic.
	 * @author Jorge Braccini
	 */
	public class ComponentContainer extends Component {
		
		private var _container:Sprite;
		
		public function ComponentContainer(init:Object = null) {
			_container = new Sprite();
			
			super(init);
		}
		
		/////////////////////////////////////////
		// Public Methods ///////////////////////
		/////////////////////////////////////////
		
		override public function addView(view:View):void {
			view.viewParent = this;
			view.addEventListener("atom_invalidate", 	handleExternalInvalidate);
			
			_views.push(view);
			content.addChild(view);
		}
		
		override public function removeView(view:View):void {
			view.viewParent = null;
			view.removeEventListener("atom_invalidate", handleExternalInvalidate);
			
			if (containsView(view)) {
				view.viewParent = null;
				
				_views.splice(_views.indexOf(view), 1);
				content.removeChild(view);
			}
		}
		
		/////////////////////////////////////////
		// Protected Methods ////////////////////
		/////////////////////////////////////////
		
		override protected function create():void {
			addChild(_container);
			super.create();
		}
		
		override protected function draw():void {
			content.x = parentMargin;
			content.y = parentMargin;
			
			drawBoundaries();
		}
		
		/////////////////////////////////////////
		// Properties ///////////////////////////
		/////////////////////////////////////////
		
		protected function get content():Sprite { return _container; }
		
		public function get margin():Number { return init.margin; }
		
		public function set margin(value:Number):void {
			init.margin = value;
			invalidate();
		}
		
		protected function get parentMargin():Number {
			return (viewParent is ComponentContainer) ? (viewParent as ComponentContainer).margin : 0;
		}
		
		/////////////////////////////////////////
		// Private Methods //////////////////////
		/////////////////////////////////////////
		
		private function drawBoundaries():void {
			graphics.clear();
			graphics.lineStyle(1, 0);
			graphics.drawRect(0, 0, width - 1, height - 1);
		}
		
		private function handleExternalInvalidate(e:Event):void {
			invalidate();
		}
		
	}

}